package com.msc.deploy;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

public class Dom4jTest {
	private static class Test{
		public void say(){
			System.out.println("----------------"); 
		}
	}
	public static Test gettest(){
		return new Test();
	}
	public static void main( String[] args )throws Exception {
		SAXReader reader      = new org.dom4j.io.SAXReader();
		Document dom         = reader.read( new File( "/tmp/test.xml") );
		Element rootObject  = dom.getRootElement();
		System.out.println( rootObject.getName()  );
		
		
		String nsUri = rootObject.getNamespaceURI();  
		Map nsMap = new java.util.HashMap();  
		nsMap.put("ns", nsUri);  
		
		 
		List<Element> elements = rootObject.elements( "ObjectList" );  
		System.out.println( elements.size() );
		
		XPath mesXpath = rootObject.createXPath( "/ns:SimManagerImport/ns:Object[@type='ModelVariant']" );  
		mesXpath.setNamespaceURIs(nsMap);  
		List<Node> variantNodes = mesXpath.selectNodes( rootObject );  
		
		XPath mesXpath2 = ( (Element)variantNodes.get( 0 ) ).createXPath( "ns:Attribute[@name='branch']" );  
		mesXpath2.setNamespaceURIs(nsMap);  
		variantNodes = mesXpath2.selectNodes( (Element)variantNodes.get( 0 ) )  ;
		
		/*
		reader.getDocumentFactory().setXPathNamespaceURIs( nsMap );
		dom         = reader.read( new File( "/tmp/test.xml") );
		rootObject  = dom.getRootElement();
		List<Node> variantNodes = rootObject.selectNodes( "/ns:SimManagerImport/ns:Object[@type='ModelVariant']" );
		*/
		
		//List<Node> variantNodes = rootObject.selectNodes( "/SimManagerImport/Object[@type='ModelVariant']" );
		
		System.out.println( variantNodes.size() );
				
	}
}
